<?php


namespace app\mobile\controller;


use think\cache\driver\Redis;
use think\Controller;

class Index extends Controller
{
    public function index()
    {
        $member_id = session('member_id') ?? null;
        if(!empty($member_id)){
            $member = db('member')->find($member_id);
            $this->assign('member',$member);
        }
        //轮播图
        $banner = $this->banner();
//        dump($banner);die;
        //精品推荐
        $recommendation = $this->recommendation();
        //追更榜
        $moreList = $this->moreList();
        //原创精品
        $original = $this->original();
        //新书榜
        $newBook = $this->newBook();
        //原创新书
        $originalNew = $this->originalNew();
        //银票榜
        $ticketList = $this->ticketList();
        //新书入库
        $news = $this->news();
        //最近更新
        $recently = $this->recently();
        $this->assign([
            'current' => 'index',
            'banner' => $banner,
            'recommendation' => $recommendation,
            'moreList' => $moreList,
            'original' => $original,
            'newBook' => $newBook,
            'originalNew' => $originalNew,
            'ticketList' => $ticketList,
            'news' => $news,
            'recently' => $recently,
            'member_id' => $member_id
        ]);
        return view("index");
    }
    //轮播
    public function banner(){
        //state == 1 的时候 轮播图为首页
        $banner = db("banner")
            ->where('state',1)
            ->cache(true,60)
            ->order('id', 'desc')
            ->limit(3)
            ->select();
        foreach($banner as $key => $value){
            $target = explode('index',$value['target']);
            $banner[$key]['target'] = 'index'.$target[1];
        }
        return $banner;
    }
    //精品推荐
    public function recommendation(){
        $book = db("book")
            ->where('state',1)
            ->cache(true,60)
            ->where('recommendation',1)
            ->order('update','desc')
            ->limit(11)->select();
        return $book;
    }
    // 追更
    public function moreList(){
        $book = db("book")
            ->where('state',1)
            ->where('more_list',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(8)->select();
        return $book;
    }
    // 原创精品
    public function original(){
        $original = db("book")
            ->where('state',1)
            ->where('original',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(18)->select();
        return $original;
    }

//    新书榜
    public function newBook(){
        $original = db("book")
            ->where('state',1)
            ->where('new_book_list',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(10)->select();
        return $original;
    }
//    原创新书
    public function originalNew(){
        $originalNew = db("book")
            ->where('original_new',1)
            ->where('state',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(15)->select();
        return $originalNew;
    }
    //银票榜
    public function ticketList(){
        $ticketList = db("book")
            ->where('ticket_list',1)
            ->where('state',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(10)->select();
        return $ticketList;
    }
//    新书入库
    public function news(){
        $news = db("book")
            ->where('state',1)
            ->where('new',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(19)->select();
        return $news;
    }
    //最近更新
    public function recently(){
        $recently = db("book")
            ->where('state',1)
            ->where('recently',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(10)->select();
        return $recently;
    }
    public function footer(){
        $data = db("web") ->cache(true,600)->find();
        return $data;
    }
    public function crontab(){
//        获取当前时间戳
        $strto_time = time()+10;
//      查询数据库中 小于当前时间戳的数据
        $min_time_data = db("crontab_chapter")->where('time','<=',$strto_time)->select();
        if(empty($min_time_data))die;
        foreach($min_time_data as $k=>$v){
            //查询这本书章节索引最大值
            $maxChapterIndex = db("chapter")->where('book_id',$v['book_id'])->max('index');
            $book = db("book")->where("id",$v['book_id'])->find();
            $data = [
                'book_id' => $v['book_id'],
                'name' => $v['name'],
                'content_id' => $v['content_id'],
                'is_free' => $book['auto_free'],
                'size' => $v['size'],
                'index' => $maxChapterIndex+1
            ];
            $res = db("chapter")->insert($data);
            if($res){
                db("crontab_chapter")->where('id',$v['id'])->delete();
            }
        }

    }

}